#Include "Fileio.ch"
#Include "Topconn.ch"
#Include "tbiconn.ch"

/*
+----------------------------------------------------------------------------+
!                             FICHA TECNICA DO PROGRAMA                      !
+----------------------------------------------------------------------------+
!Programa          ! CPL05DMS                                                !
+------------------+---------------------------------------------------------+
!Descricao         ! DMS                                                     !
!                  ! ImportCabinetInventory.                                 !
!                  ! Inventario de conservadoras                             !
!                  !                                                         !
+------------------+---------------------------------------------------------+
!Autor             ! TSC505 - Felipe Jose Limas                              !
+------------------+---------------------------------------------------------+
!Data de Criacao   ! 31/05/12                                                !
+------------------+---------------------------------------------------------+
!   ATUALIZACOES                                                             !
+-------------------------------------------+-----------+-----------+--------+
!   Descricao detalhada da atualizacao      !Nome do    ! Analista  !Data da !
!                                           !Solicitante! Respons.  !Atualiz.!
+-------------------------------------------+-----------+-----------+--------+
!                                           !           !           !        !
!                                           !           !           !        !
+-------------------------------------------+-----------+-----------+--------+
!                                           !           !           !        !
!                                           !           !           !        !
+-------------------------------------------+-----------+-----------+--------+
*/


User Function CPL05DMS()
Local nHandle  :=-1
Local cLinha   :=""
Local cQuery   :=""
Local cAli     :=GetNextAlias()
Local cArq     :=StrTran("\dms\gerados\ImportCabinetInventory_"+DtoS(dDataBase)+Time()+".txt", ":", "")

//campos do arquivo
Local cdCabinet      :=""
Local cdStore        :=""
Local cdStatus       :=""
Local dtInventory    :=""
Local dsGluingStatus :=""
Local dsLocationStore:=""
Local nQtdR := 0

cQuery:= " SELECT T9_CLIENTE,T9_LOJACLI ,T9_STATUS,T9_LOCAL, T9_DADESIV, T9_LOJACLI, T9_CODBEM, T9_PROPRI,T9_ZDTALT,T9_CODBEM,T9_ZDTINVE "
cQuery+= " FROM "+RetSqlName("ST9")+" ST9 "
cQuery+= " WHERE T9_PROPRI IN ('K', 'D') AND "
cQuery+= "       "+RetSqlCond("ST9")
cQuery+= " AND T9_ZALTDMS = 'S' "
cQuery+= " AND T9_SITMAN = 'A'  "
cQuery+= " AND T9_ZINCDMS = 'S'"

TCQuery ChangeQuery(cQuery) New Alias &(cAli)
Count to nQtdR

If nQtdR > 0
	nHandle:= FCREATE(cArq)
	If nHandle == -1
		conout("Erro ao criar arquivo - ferror " + Str(Ferror()))
	Else
		&(cAli)->(dbGoTop())
		Do While !&(cAli)->(EOF())
			If &(cAli)->(T9_PROPRI) == "K"
				cdCabinet:= Alltrim("2236"+Alltrim(&(cAli)->(T9_CODBEM)))+Chr(9)
			Else
				cdCabinet:= Alltrim(&(cAli)->(T9_CODBEM))+Chr(9)
			EndIf
			
			czCod:= Alltrim(&(cAli)->(T9_CLIENTE)) + Alltrim(&(cAli)->(T9_LOJACLI))
			If czCod <> ""
				czCod:= Alltrim(&(cAli)->(T9_CLIENTE)) + Alltrim(&(cAli)->(T9_LOJACLI))
			EndIf
			cdStore         := Alltrim(czCod)+Chr(9)
			
			do case
				Case Alltrim(&(cAli)->(T9_STATUS)) = "AL"
					cdStatus:= Padr("ALOC",5)+Chr(9)
				Case Alltrim(&(cAli)->(T9_STATUS)) = "US"
					cdStatus:= Padr("USADA",5)+Chr(9)
				Case Alltrim(&(cAli)->(T9_STATUS)) = "NV"
					cdStatus:= Padr("NOVA",5)+Chr(9)
				Case Alltrim(&(cAli)->(T9_STATUS)) = "RP"
					cdStatus:= Padr("REPLA",5)+Chr(9)
				Case Alltrim(&(cAli)->(T9_STATUS)) = "MN"
					cdStatus:= Padr("MANUT",5)+Chr(9)
				Case Alltrim(&(cAli)->(T9_STATUS)) = "DS"
					cdStatus:= Padr("DESCA",5)+Chr(9)
				Case Alltrim(&(cAli)->(T9_STATUS)) = "PD"
					cdStatus:= Padr("PERD",5)+Chr(9)
			EndCase
			If !Alltrim(&(cAli)->(T9_ZDTINVE)) = ""
				dtInventory     := Left(&(cAli)->(T9_ZDTINVE), 4)+"-"+SubStr(&(cAli)->(T9_ZDTINVE), 5, 2)+"-"+Right(&(cAli)->(T9_ZDTINVE), 2)+Chr(9)
			Else
				dtInventory     := Alltrim(Left(DtoS(dDataBase), 4)+"-"+SubStr(DtoS(dDataBase), 5, 2)+"-"+Right(DtoS(dDataBase), 2))+Chr(9)
			EndIF
			dsGluingStatus  := Padr(&(cAli)->(T9_DADESIV),5)+Chr(9)
			dsLocationStore := Padr(&(cAli)->(T9_LOCAL),5)+Chr(9)
			cLinha          := cdCabinet+cdStore+cdStatus+dtInventory+dsGluingStatus+dsLocationStore
			FWrite(nHandle, cLinha+Chr(13)+Chr(10))
			&(cAli)->(dbSkip())
		EndDo
		
		TcSqlExec("UPDATE "+RetSqlName("ST9")+" SET T9_ZALTDMS = 'N' ")
		
		&(cAli)->(dbCloseArea())
		FClose(nHandle)
		
		
		/*Tenta se conectar ao servidor ftp na porta 21
		//conexao sera feita com programa externo ws_ftp - gabriel 17/10/2012
		If !FTPCONNECT("189.59.223.136" , 21 ,"FTP_UDB_CPL", "16Access97")
			conout("Nao foi possivel conectar")
			Return .F.
		EndIf
		
		If !FTPUPLOAD(cArq, "/FTP_UDB_CPL/Inbox/"+SubStr(cArq, At("IMPORT", UPPER(cArq))))
			conout("Nao foi possivel realizar o upload!!")
			Return .F.
		EndIf
		FTPDISCONNECT() 
		*/
	EndIf
EndIf
Return cArq